#!/usr/bin/env python
#coding=utf-8
from bs4 import BeautifulSoup
import lxml.html
import lxml.cssselect
import urllib2
def download(url, user_agent='wswp', num_retries=2):
	print 'Downloading:', url
	headers = {'User-agent':user_agent}
	request = urllib2.Request(url, headers=headers)
	try:
		html = urllib2.urlopen(request).read()
		#print html
	except urllib2.URLError as e:
		print('Download error:', e.reason)
		html = None
		if num_retries > 0:
			if hasattr(e, 'code') and 500 <= e.code < 600:
				#retyr 5XX HTTP errors
				return download(url, user_agent, num_retries-1)
	return html

url = 'http://example.webscraping.com/places/default/view/Cuba-55'
v = download(url)
tree = lxml.html.fromstring(v)
td = tree.cssselect('tr#places_population__row > td.w2p_fw')[0]
population = td.text_content()
print population
